﻿Imports NewHeaderSystems.Archive.Core
Imports NewHeaderSystems.Archive.Data

Public Class SecurityMaintenanceEditor


    Private _securityView As SecurityView
    Public Property SecurityView() As SecurityView
        Get
            Return _securityView
        End Get
        Set(ByVal value As SecurityView)
            _securityView = value
        End Set
    End Property
    Public Sub New(ByVal securityView As SecurityView)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        DialogResult = Windows.Forms.DialogResult.Cancel 'Set default user might click close button on title

        ' Add any initialization after the InitializeComponent() call.
        _securityView = SecurityView
    End Sub

    Private Sub txtTicker_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTicker.Validating
        ValidateRequired(txtTicker, "Please enter a Ticker")
    End Sub

    Private Sub txtCusip_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCusip.Validating
        ValidateRequired(txtCusip, "Please enter a CUSIP")
    End Sub

    Private Sub ValidateRequired(ByVal textBox As TextBox, ByVal message As String)
        Dim isValid As Boolean = Not String.IsNullOrEmpty(textBox.Text) And textBox.Text.Trim().Length > 0
        If isValid Then
            ErrorProvider.SetError(textBox, String.Empty)
        Else
            ErrorProvider.SetError(textBox, message)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If ValidForm() Then
            If DataScript.IsExistingTicker(_securityView.ID, txtTicker.Text) Then
                MessageBox.Show("This Ticker already exists, please type in different ticker.", "Duplicate Ticker", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txtTicker.Focus()
            ElseIf DataScript.IsExistingCusip(_securityView.ID, txtCusip.Text) Then
                MessageBox.Show("This CUSIP already exists, please type in different CUSIP.", "Duplicate CUSIP", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txtCusip.Focus()
            Else
                DataScript.SaveSecurity(_securityView.ID, txtTicker.Text, txtCusip.Text, txtDescription.Text)
                DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
            End If
        End If
    End Sub

    Private Function ValidForm() As Boolean
        Return String.IsNullOrEmpty(ErrorProvider.GetError(txtTicker)) And String.IsNullOrEmpty(ErrorProvider.GetError(txtCusip))
    End Function

    Private Sub SecurityMaintenanceEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtTicker.Text = _securityView.Ticker
        txtCusip.Text = _securityView.Cusip
        txtDescription.Text = _securityView.Description
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub txtTicker_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTicker.Leave
        txtTicker.Text = txtTicker.Text.ToUpper()
    End Sub
End Class